package com.redhat.auth.log.core.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.dayatang.domain.InstanceFactory;
import com.redhat.auth.log.core.LogBizQuery;
import com.redhat.auth.log.core.domain.BizLogInfo;
import com.redhat.auth.log.query.LogQueryChannelImpl;
/**
 * 业务框架提供的一个默认存储实现,将一个日志对象存入数据库
 * @author lingen.liu
 *
 */
@Service(value = "logBizSave")
public class LogBizQueryImpl  extends HibernateDaoSupport implements LogBizQuery {
	
	@Override
	public void add(BizLogInfo bizLogInfo) {
		this.getHibernateTemplate().save(bizLogInfo);
	}
	
	/**
	 * 返回一个BizLogInfo
	 * @param logId
	 * @return
	 */
	@Override
	public BizLogInfo getBizLogInfo(long logId){
		return this.getHibernateTemplate().get(BizLogInfo.class, logId);
	}
	
	/**
	 * 返回此所有业务操作类型
	 * @return
	 */
	@Override
	public List<String> getBizOperation(){
		String sql = "select distinct bizOperation from BizLogInfo";
		List<String> bizTypeList = this.getHibernateTemplate().find(sql);
		return bizTypeList;
	}
	
	/**
	 * 返回某个业务类型下的子操作类型
	 * @param bizDescription
	 * @return
	 */
	@Override
	public List<String> getBizTypes(String bizOperation){
		String sql = "select distinct bizType from BizLogInfo where bizOperation = '"+bizOperation+"'";
		List<String> bizTypeList = this.getHibernateTemplate().find(sql);
		return bizTypeList;
	}
	
	/**
	 * 根据指定业务名，业务操作类型，查询出对应的业务记录
	 * @param bizOperation
	 * @param bizType
	 * @return
	 */
	@Override
	public List<BizLogInfo> getBizLogInfo(String bizOperation,String bizType){
		DetachedCriteria criteria = DetachedCriteria.forClass(BizLogInfo.class);
		criteria.add(Restrictions.eq("bizOperation", bizOperation));
		if(bizType!=null && !bizType.equals(""))criteria.add(Restrictions.eq("bizType", bizType));
		List<BizLogInfo> logs = this.getHibernateTemplate().findByCriteria(criteria);
		return logs;
	}

}
